<?php

require_once( 'mvc/dynamo-info.php' );

require_once( 'flexical/admin/request-factory.php' );
require_once( 'flexical/admin/constants.php' );
require_once( 'flexical/admin/null-request.php' );
require_once( 'flexical/admin/error-request.php' );

class Flexical_Admin_Main_Request_Factory extends Flexical_Admin_Request_Factory
{
	private $factories = array();

	public function __construct()
	{
		$info = new MVC_Dynamo_Info( 'flexical/admin/about-request-factory.php', 'Flexical_Admin_About_Request_Factory' );
		$this->factories[ 'show_about' ] = $info;

		$info = new MVC_Dynamo_Info( 'flexical/admin/feed-request-factory.php', 'Flexical_Admin_Feed_Request_Factory' );
		$this->factories[ 'show_feeds' ] = $info;
		$this->factories[ 'create_feed' ] = $info;
		$this->factories[ 'edit_feed' ] = $info;
		$this->factories[ 'update_feed' ] = $info;
		$this->factories[ 'delete_feed' ] = $info;
		$this->factories[ 'confirm_delete_feed' ] = $info;
		$this->factories[ 'test_feed' ] = $info;
		$this->factories[ 'test_all_feeds' ] = $info;
		$this->factories[ 'delete_all_feeds' ] = $info;
		$this->factories[ 'import_feeds' ] = $info;

		$info = new MVC_Dynamo_Info( 'flexical/admin/query-request-factory.php', 'Flexical_Admin_Query_Request_Factory' );
		$this->factories[ 'show_queries' ] = $info;
		$this->factories[ 'create_query' ] = $info;
		$this->factories[ 'edit_query' ] = $info;
		$this->factories[ 'update_query' ] = $info;
		$this->factories[ 'confirm_delete_query' ] = $info;
		$this->factories[ 'delete_query' ] = $info;
		$this->factories[ 'delete_all_queries' ] = $info;

		$info = new MVC_Dynamo_Info( 'flexical/admin/format-request-factory.php', 'Flexical_Admin_Format_Request_Factory' );
		$this->factories[ 'show_formats' ] = $info;
		$this->factories[ 'create_format' ] = $info;
		$this->factories[ 'update_format' ] = $info;
		$this->factories[ 'delete_format' ] = $info;
		$this->factories[ 'edit_format' ] = $info;
		$this->factories[ 'view_format' ] = $info;
		$this->factories[ 'confirm_delete_format' ] = $info;
		$this->factories[ 'delete_all_formats' ] = $info;

		$info = new MVC_Dynamo_Info( 'flexical/admin/listing-request-factory.php', 'Flexical_Admin_Listing_Request_Factory' );
		$this->factories[ 'show_listings' ] = $info;
		$this->factories[ 'create_listing' ] = $info;
		$this->factories[ 'update_listing' ] = $info;
		$this->factories[ 'delete_listing' ] = $info;
		$this->factories[ 'delete_all_listings' ] = $info;
		$this->factories[ 'confirm_delete_listing' ] = $info;
		$this->factories[ 'edit_listing' ] = $info;
		$this->factories[ 'test_listing' ] = $info;
	}

	public function get_request()
	{
		$request = null;

		$request_type = $this->get_request_type( $_REQUEST );

		if ( $request == null && isset( $this->factories[ $request_type ] ) )
		{
			$request = $this->factories[ $request_type ]->new_instance()->get_request();
		}

		else
		{
			return new Flexical_Admin_Unknown_Request_Type_Request( $request_type );
		}

		if ( $this->action_needs_authorization( $request_type ) )
		{
			check_admin_referer( $request_type );
		}

		return $request;
	}

	private function action_needs_authorization( $request_type = null )
	{
		$no_auth_needed = array(
			'show_about',
			'show_feeds',
			'show_queries',
			'show_formats',
			'show_listings',
		);

		return ! in_array( $request_type, $no_auth_needed );
	}
}

?>
